package com.app.base.service.file.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.app.base.model.vo.PersonnelVo;
import com.app.base.service.core.personnel.entity.PersonInfo;
import com.app.base.service.core.personnel.service.PersonInfoService;
import com.app.base.service.file.service.ExportExcelService;
import com.app.base.utlis.data.PersonInfoUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ExportExcelServiceImpl implements ExportExcelService {


    @Resource
    private PersonInfoService personInfoService;

    @Override
    public void reportPersonnelExcel(HttpServletResponse response, String educationStr, String startTime, String endTime, PersonInfo person) {

        try{
            QueryWrapper<PersonInfo> wrapper = new QueryWrapper<>();
            if (!StringUtils.isEmpty(startTime)){
                wrapper.between("create_time",startTime,endTime);
            }

            if (!person.getName().equals("undefined")){
                wrapper.like("name",person.getName());
            }

            if (!educationStr.equals("undefined")){
                wrapper.eq("education",Integer.parseInt(educationStr));
            }

            if (!person.getExpectedPosition().equals("undefined")){
                wrapper.like("expected_position",person.getExpectedPosition());
            }

            if (!person.getGraduationSchool().equals("undefined")){
                wrapper.like("graduation_school",person.getGraduationSchool());
            }

            List<PersonInfo> personInfoList = personInfoService.list(wrapper);

            List<PersonnelVo> personnelVos = new ArrayList<>();

            for (PersonInfo personInfo : personInfoList) {
                    PersonnelVo personnelVo = new PersonnelVo();
                    BeanUtils.copyProperties(personInfo, personnelVo);
                    personnelVo.setGender(personInfo.getSex()== 0 ? "男":"女");
                    String educations = PersonInfoUtils.getEducations(personInfo);
                    personnelVo.setEducations(educations);
                    String countenance = PersonInfoUtils.getCountenance(personInfo);
                    personnelVo.setCountenance(countenance);
                    personnelVos.add(personnelVo);
            }

            String formatDate = new SimpleDateFormat("yyyyMMdd_HH-mm-ss").format(new Date());

            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
            HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode(formatDate + "-人才信息表", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), PersonnelVo.class)
                    .autoCloseStream(Boolean.FALSE)
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .sheet("人才信息表" )
                    .doWrite(personnelVos);

        }catch(Exception e){
            log.error(e.getMessage());
        }

    }


}
